title = "tx"
url = "/tx/:txhash?"
layout = "mgc"
is_hidden = 0
==

<?php

use Brick\Math\BigInteger;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;

function onStart() {

	//总输出
	$this['allOut'] = 0;

	if($this->param('txhash')){

		//交易哈希、大小、接收时间、开采时间、所属块、表示块奖励?
		$details = Db::connection('magnachain')->table('transaction')->select('txhash', 'txsize', 'blockhash')->where('txhash', $this->param('txhash'))->get();
		$details = $details->toArray();

		if($details == null){

			header("Location: /404/找不到交易~");		//404处理
			exit();

		}             
		
		$block = Db::connection('magnachain')->table('block')->select('height', 'time')->where('blockhash', $details[0]->blockhash)->get();
		$block = $block->toArray();

		$details[0]->height = $block[0]->height;
		$details[0]->time = date("Y-m-d H:i:s", $block[0]->time);

		$this['details'] = $details;

		//交易完成时间
		$this['time'] = $details[0]->time;

		//存地址和输入、输出
		$in = array();
		$out = array();

		$distinct_address = Db::connection('magnachain')->table('txoutpubkey')->select('solution')->where('txhash', $this->param('txhash'))->distinct()->get();
		$distinct_address = $distinct_address->toArray();

		for($i = 0; $i < count($distinct_address); $i++){
		
			$out[$i]['address'] = $distinct_address[$i]->solution;
			$out[$i]['outNum'] = "0";
			
		}

		$records = array();

		$In = BigDecimal::of('0'); 	//存该交易的输入和输出
		$Out = BigDecimal::of('0');

		$allIn = BigDecimal::of('0');  	//存该交易的总输入和总输出
		$allOut = BigDecimal::of('0');

		//获取交易详情
		$txoutpubkey = Db::connection('magnachain')->table('txoutpubkey')
	    ->join('txout', function ($join) {
	        $join->on('txoutpubkey.txindex', '=', 'txout.txindex')->on('txoutpubkey.txhash', '=', 'txout.txhash');
	    })
	    ->select('txoutpubkey.solution', 'txoutpubkey.txindex', 'txout.value')
	    ->where('txoutpubkey.txhash', '=', $this->param('txhash'))
	    ->get();

		for($i = 0; $i < count($txoutpubkey); $i++){

			for($j = 0; $j < count($out); $j++){

				if($txoutpubkey[$i]->solution == $out[$j]['address']){

					$out[$j]['outNum'] = BigDecimal::of($out[$j]['outNum'])->plus(BigDecimal::of($txoutpubkey[$i]->value)->dividedBy('100000000', 2, RoundingMode::HALF_DOWN));
					$out[$j]['outNum'] = strval($out[$j]['outNum']);

					$allOut = BigDecimal::of($allOut)->plus($txoutpubkey[$i]->value);

				}

			}

		}

		/*//获取交易详情
		$txoutpubkey = Db::connection('magnachain')->table('txoutpubkey')->select('solution', 'txindex')->where('txhash', $this->param('txhash'))->distinct()->get();
		$txoutpubkey = $txoutpubkey->toArray();

		for($i = 0; $i < count($txoutpubkey); $i++){

			for($j = 0; $j < count($out); $j++){

				if($txoutpubkey[$i]->solution == $out[$j]['address']){

					$txout = Db::connection('magnachain')->table('txout')->select('value')->where('txhash', $this->param('txhash'))->where('txindex', $txoutpubkey[$i]->txindex)->get();
					$txout = $txout->toArray();

					$out[$j]['outNum'] = BigDecimal::of($out[$j]['outNum'])->plus(BigDecimal::of($txout[0]->value)->dividedBy('100000000', 2, RoundingMode::HALF_DOWN));
					$out[$j]['outNum'] = strval($out[$j]['outNum']);

					$allOut = BigDecimal::of($allOut)->plus($txout[0]->value);

				}

			}

		}*/

		$txin = Db::connection('magnachain')->table('txin')->select('outpointhash', 'outpointindex')->where('txhash', $this->param('txhash'))->get();
		$txin = $txin->toArray();

		if(empty($txin)){

			$this['reward'] = true;
			$allIn = null;

		} else {

			for($i=0; $i < count($txin); $i++){

				$in[$i]['address'] = "";
				$in[$i]['inNum'] = "0";

				$previous_txin = Db::connection('magnachain')->table('txout')->select('value', 'txhash', 'txindex')->where('txhash', $txin[$i]->outpointhash)->where('txindex', $txin[$i]->outpointindex)->get();
				$previous_txin = $previous_txin->toArray();

				$in[$i]['inNum'] = BigDecimal::of($in[$i]['inNum'])->plus(BigDecimal::of($previous_txin[$i]->value)->dividedBy('100000000', 2, RoundingMode::HALF_DOWN));
				$in[$i]['inNum'] = strval($in[$i]['inNum']);
		
				$previous_address = Db::connection('magnachain')->table('txoutpubkey')->select('solution')->where('txhash', $previous_txin[$i]->txhash)->where('txindex', $previous_txin[$i]->txindex)->get();
				$previous_address = $previous_address->toArray();

				$in[$i]['address'] = $previous_address[0]->solution;

				$allIn = BigDecimal::of($allIn)->plus($previous_txin[$i]->value);

			}
	
		}

		if (count($out) > 5) {

			$out = array_slice($out, 0, 5);
			$records[0]['more_output'] = true;
			
		}

		if (count($in) > 5) {

			$in = array_slice($in, 0, 5);
			$records[0]['more_input'] = true;
			
		}

		if ($allIn != null) {
			
			$allIn = $allIn->dividedBy('100000000', 2, RoundingMode::HALF_DOWN);
			$allOut = $allOut->dividedBy('100000000', 2, RoundingMode::HALF_DOWN);

			$allIn = strval($allIn);
			$allOut = strval($allOut);

			$free = floatval($allIn) - floatval($allOut);
			$free = sprintf("%.2f", $free);

			$this['free'] = $free;

		} else {

			$allOut = $allOut->dividedBy('100000000', 2, RoundingMode::HALF_DOWN);
			$allOut = strval($allOut);
		}

		$records[0]['in'] = $in;
		$records[0]['out'] = $out;

		$records[0]['allOut'] = $allOut;
		$records[0]['txhash'] = $this->param('txhash');

		$this['records'] = $records;
		
	} else {

		header("Location: /404/请正确输入交易哈希~");

	}

}

?>
==
<div class="container content">
	
	<div class="table-responsive">

		<table class="table">
			<span class="ng-scope" style="font-size: 24px; float: left; margin-top: -20px; margin-bottom: 20px;">{{ '交易详情'|_ }}</span>
			<tbody class="tbody">
			
			{% for detail in details %}

		     <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '交易哈希'|_ }}</span></td>
		        <td><span style="margin-top: 7px; float: right;">{{ detail.txhash }}</span></td>
		     </tr>

			 <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '大小'|_ }}</span></td>
		        <td><span style="margin-top: 7px; float: right;">{{ detail.txsize }} Bytes</span></td>
		     </tr>

		     <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '接收时间'|_ }}</span></td>
		        <td><span style="margin-top: 7px; float: right;">{{ detail.time }}</span></td>
		     </tr>

		     <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '开采时间'|_ }}</span></td>
		        <td><span style="margin-top: 7px; float: right;">{{ detail.time }}</span></td>
		     </tr>

		     <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '所属块'|_ }}</span></td>
		        <td>
		        	<a href="/block/{{ detail.height }}" style="margin-top: 7px; float: right;">
						<span class="ellipsis ng-binding">{{ detail.height }}</span>
					</a>
				</td>
		     </tr>

			{% if reward == true %}
		     <tr style="height: 51px;">
		        <td><span style="margin-top: 7px; float: left; font-weight: bold;">{{ '挖矿奖励交易'|_ }}</span></td>
		        <td><span style="margin-top: 7px; float: right;">{{ '是'|_ }}</span></td>
		     </tr>
			{% endif %}

			{% endfor %}

		  </tbody>

		</table>

	</div>

	<h1 style="font-size: 24px; text-align: left; margin-bottom: 20px; margin-top: 40px;">{{ '交易记录'|_ }}</h1>

	<div class="tx-record">

		{% for record in records %}
		<div class="box row line-mid ng-scope" style="margin-top: 20px; border: 1px solid #ebebeb;">
			
			<a class="hidden-xs hidden-sm" href="javascript:void(0)" style="float: left; margin-top: 14px; margin-bottom: 14px; margin-left: 40px;">
				<span class="ellipsis">{{ record.txhash }}</span>
			</a>
		
			<a class="hidden-md hidden-lg" href="#" style="float: left; margin-top: 14px; margin-bottom: 14px; margin-left: 20px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; width: 200px;">
				<span class="ellipsis">{{ record.txhash }}</span>
			</a>
			
			<span class="hidden-xs hidden-sm" style="float: right; margin-top: 14px; margin-bottom: 14px; margin-right: 60px;">{{ '完成时间'|_ }} {{ time }}</span>
		
			<span class="hidden-md hidden-lg" style="float: right; margin-top: 14px; margin-bottom: 14px; margin-right: 20px;">{{ '完成时间'|_ }} {{ time }}</span>

			<div style="margin-top: 49px; height: 1px; background-color: #ebebeb;"></div>
		
			<div class="row">
				{% if record.in %}

				<div class="col-md-5 col-xs-12 hidden-xs hidden-sm" style="margin-top: 20px; margin-left: 40px; float: left;">

				<div id="input_div_{{ record.txhash }}">
					{% for txin in record.in %}

					<div class="panel panel-default" id="input" style="background-color: #ebebeb; padding-top: 12px; padding-left: 12px;"><a href="/address/{{ txin.address }}" style="float: left;">{{ txin.address }}</a><p>{{ txin.inNum }} MGC</p></div>

					{% endfor %}
				</div>

				{% if record.more_in %}

				<button class="btn btn-default btn-md" id="btn_input_{{ record.txhash }}" style="float: left;" status="1">{{ '显示更多'|_ }}</button>

				{% endif %}

				</div>
		
				<div class="col-md-5 col-xs-12 hidden-md hidden-lg" style="margin-top: 20px;">

					<div id="input_div_{{ record.txhash }}">
						{% for txin in record.in %}

						<div class="panel panel-default" id="input" style="background-color: #ebebeb; padding-top: 12px; padding-left: 12px;"><a href="/address/{{ txin.address }}" style="float: left;">{{ txin.address }}</a><p>{{ txin.inNum }} MGC</p></div>

						{% endfor %}
					</div> 

					{% if record.more_in %}

					<button class="btn btn-default btn-md" id="btn_input_{{ record.txhash }}" style="float: left;" data-status="1">{{ '显示更多'|_ }}</button>

					{% endif %}

				</div>

				{% else %}

				<div class="col-md-5 col-xs-12 hidden-xs hidden-sm" style="margin-top: 20px; margin-left: 40px;">

					<div class="panel panel-default" style=" background-color: #ebebeb; padding-top: 12px; padding-left: 12px;"><p>{{ '没有交易输入'|_ }}</p></div>

				</div>

				<div class="col-md-5 col-xs-12 hidden-md hidden-lg" style="margin-top: 20px;">

					<div class="panel panel-default" style=" background-color: #ebebeb; padding-top: 12px; padding-left: 12px;"><p>{{ '没有交易输入'|_ }}</p></div>

				</div>

				{% endif %}

				<div class="col-md-1 col-xs-12" style="text-align: center; margin-top: 10px;">

					<div class="hidden-xs hidden-sm"><span style="font-size: 34px; color: #ebebeb;">＞</span></div>

					<div class="hidden-md hidden-lg"><span style="font-size: 34px; color: #ebebeb;">∨</span></div>

				</div>
				
				<div class="col-md-5 col-xs-12" style="margin-top: 20px;">

					<div id="output_div_{{ record.txhash }}">
						{% for out in record.out %}

						<div class="panel panel-default" style="background-color: #ebebeb; padding-top: 12px; padding-left: 12px;"><a href="/address/{{ out.address }}" style="float: left;">{{ out.address }}</a><p>{{ out.outNum }} MGC</p></div>

						{% endfor %}
					</div>

					{% if record.more_output %}

					<button class="btn btn-default btn-md" id="btn_output_{{ record.txhash }}" style="float: left;">{{ '显示更多'|_ }}</button>

					{% endif %}

				</div>
					
			</div>
		
			<div style="border-top: 1px solid #ebebeb; margin-top: 10px;">
				{% if free %}
					<div>
						<button type="button" class="btn btn-default btn-sm" disabled="disabled" style="float: left; margin-left: 10px; margin-bottom: 10px; margin-top: 10px;">{{ '矿工费'|_ }}： {{ free }} MGC</button>
					</div>
				{% endif %}
				<div>
					<button type="button" class="btn btn-default btn-sm" disabled="disabled" style="float: right; margin-right: 10px; margin-bottom: 10px; margin-top: 10px;">{{ record.allOut }} MGC</button>
				</div>
			</div>
		
		</div>
		{% endfor %}

	</div>

</div>

<script>
	
	$("button[id^='btn_input_'").bind("click", function(event){

		event.stopImmediatePropagation();

		var txhash = event.target.id;
		var txhash = txhash.substring(10);

		var input_div = '#input_div_' + txhash;

		var text = $('#btn_input_'+txhash).text();

		if (text == "显示更多") {

			$.post("/more_input", {txhash:txhash}, function (data, status) {

				var data = JSON.parse(data);
				
				$('#btn_input_'+txhash).text('显示更少');

				for(var i = 0; i < data.length; i++){

					$(input_div).append("<div class='panel panel-default' id='more_input_" + txhash + "' style='background-color: #ebebeb; padding-top: 12px; padding-left: 12px;'><a href='/address/" + data[i]['address'] + "' style='float: left;'>" + data[i]['address'] + "</a><p>" + data[i]['inNum'] + " MGC</p></div>");

				}
		   		
			});

		} else {

			$("div[id^='more_input_'").css('display', 'none').attr('id', 'input_hidden');
			$('#btn_input_'+txhash).text('显示更多');

		}
		
	});

	$("button[id^='btn_output_'").bind("click", function(event){

		event.stopImmediatePropagation();

		var txhash = event.target.id;
		var txhash = txhash.substring(11);

		var output_div = '#output_div_' + txhash;

		var text = $('#btn_output_'+txhash).text();

		if (text == "显示更多") {

			$.post("/more_output", {txhash:txhash}, function (data, status) {

				var data = JSON.parse(data);

				$('#btn_output_'+txhash).text('显示更少');

				for(var i = 0; i < data.length; i++){

					console.log(data[i]['address']);

					$(output_div).append("<div class='panel panel-default' id='more_output_" + txhash + "' style='background-color: #ebebeb; padding-top: 12px; padding-left: 12px;'><a href='/address/" + data[i]['address'] + "' style='float: left;'>" + data[i]['address'] + "</a><p>" + data[i]['outNum'] + " MGC</p></div>");

				}
		   		

			});

		} else {

			$("div[id^='more_output_'").css('display', 'none').attr('id', 'input_hidden');
			$('#btn_output_'+txhash).text('显示更多');

		}
		
	});


</script>